"""
Case Type   : 资源池化支持xlog归档
Case Name   : 设置资源池化主机xlog归档GUC参数，测试xlog归档功能
Create At   : 2025/06/07
Owner       : @hwworkholic
Description :
    1.配置归档参数
    2.检查归档目录中的文件，在主机上查询当前xlog位置，强制切换xlog文件
    3.检查归档目录中的文件
Expect      :
    1.成功
    2.当前归档目录没有文件
    3.当前归档目录中有第2步查询到的xlog文件
History     :
"""
import os
import unittest
import time

from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.ComThread import ComThread

from yat.test import Node
from yat.test import macro

class XLogArchiveFunc(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.com = Common()
        self.constant = Constant()
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.primary_node = Node("PrimaryDbUser")
        self.archive_command = 'dsscmd cp -s %p -d %f'
        self.archive_dest = os.path.join(macro.DB_INSTANCE_PATH, "archive_dir/")
        self.default_archive_mode = self.com.show_param("archive_mode")
        self.default_archive_dest = self.com.show_param("archive_dest")

    def test_xlog_archive(self):
        step = '-----step1:检查集群初始状态; expect:集群状态正常-----'
        self.log.info(step)
        status = self.pri_sh.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败' + step)

        step = "-----step2:设置archive_mode=on expect:成功-----"
        self.log.info(step)
        guc_res = self.pri_sh.execute_gsguc(
            "reload", self.constant.GSGUC_SUCCESS_MSG,
            f"archive_mode=on")
        self.log.info(guc_res)
        self.assertTrue(guc_res, "执行失败:" + step)

        step = "-----step3:设置archive_command expect:成功-----"
        self.log.info(step)
        guc_res = self.pri_sh.execute_gsguc(
            "reload", self.constant.GSGUC_SUCCESS_MSG,
            f"archive_command='{self.archive_command}'")
        self.log.info(guc_res)
        self.assertTrue(guc_res, "执行失败:" + step)

        step = "-----step4:设置archive_dest expect:成功-----"
        self.log.info(step)
        guc_res = self.pri_sh.execute_gsguc(
            "reload", self.constant.GSGUC_SUCCESS_MSG,
            f"archive_dest='{self.archive_dest}'")
        self.log.info(guc_res)
        self.assertTrue(guc_res, "执行失败:" + step)

        step = "-----step5:执行sql获取当前xlog位置，执行pg_switchxlog expect:成功-----"
        self.log.info(step)
        sql_res = self.pri_sh.execut_db_sql("select file_name from pg_xlogfile_name_offset(pg_current_xlog_location());")
        self.log.info(sql_res)
        self.assertIn("1 row", sql_res, "执行失败:" + step)
        sql_res = self.pri_sh.execut_db_sql("select pg_switch_xlog();")
        self.log.info(sql_res)
        self.assertIn("1 row", sql_res, "执行失败:" + step)

        step = "-----step6:检查归档目录内容 expect:成功-----"
        self.log.info(step)
        cmd = f"ls {self.archive_dest}"
        self.log.info(cmd)
        ls_archive = self.primary_node.sh(cmd).result()
        self.log.info(ls_archive)
        archive_bool = 'No such file or directory' not in ls_archive and len(
            ls_archive.split()) > 0
        self.assertTrue(archive_bool, "执行失败:" + step)

    def tearDown(self):
        self.log.info('----恢复环境----')
        rm_cmd = f"rm -rf {self.archive_dest};"
        self.log.info(rm_cmd)
        rm_msg = self.primary_node.sh(rm_cmd).result()
        self.log.info(rm_msg)
        guc_res = self.pri_sh.execute_gsguc(
            "reload", self.constant.GSGUC_SUCCESS_MSG,
            f"archive_mode={self.default_archive_mode}")
        self.log.info(guc_res)
        self.assertTrue(guc_res, "恢复guc参数失败")
        guc_res = self.pri_sh.execute_gsguc(
            "reload", self.constant.GSGUC_SUCCESS_MSG,
            f"archive_dest='{self.default_archive_dest}'")
        self.log.info(guc_res)
        self.log.info(f'----{os.path.basename(__file__)} end')
